﻿using AutoChess;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerInfoListPanel : MonoBehaviour
{
    public AutoChessMainPage mainPage;
    public PlayerInfoItemEx prefab;
    public Transform root;

    public Vector3 startPos = new Vector3(93,-53,0);
    public float height = -65;

    public Dictionary<long, PlayerInfoItemEx> playerCache = new Dictionary<long, PlayerInfoItemEx>();
    public List<Vector3> rankPos = new List<Vector3>();

    public TweenerCtrl tweenCtr;
    //初始化
    public void InitPanel()
    {
        var playerList = AutoChess.AutoChessManager.Ins.cacheData.GetAllPlayerList();
        for (int i = 0; i < playerList.Count; i++)
        {
            var item = Instantiate<PlayerInfoItemEx>(prefab, root);
            rankPos.Add(new Vector3(startPos.x, startPos.y + i * height, startPos.z));//1-8名
            item.transform.localPosition = rankPos[i];
            item.gameObject.SetActive(true);
            item.Init(playerList[i]);
            playerCache[playerList[i].GUID] = item;
        }
    }

    public void OpenPanel()
    {
        tweenCtr.PlayForward();
        mainPage.SwitchCurPanel(ACMainPagePanelType.PlayerListInfo);
    }
    public void ClosePanel()
    {
        tweenCtr.PlayReverse();
    }

    //刷新排名
    public void RefreshRank()
    {
        var playerList = AutoChess.AutoChessManager.Ins.cacheData.GetAllPlayerList();
        for (int i = 0; i < playerList.Count; i++)
        {
            playerCache[playerList[i].GUID].PlayTween(rankPos[i], playerList[i]);
        }
    }
}
